package com.shouhuobao.take.utils;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by joker on 2017/5/26.
 * 用于处理字符串的格式工具类
 */
public class StringUtils {

    /**
     * 将一段字符串的数字根据类型拆分成有空格的字符串
     * 比如编号：12334444555566 拆分成 1233 4444 5555 66
     * 手机号：13546447892 拆分成  135 4644 7892
     *
     * @param type   1 手机号  2 其他编号
     * @param numStr 要拆分的数字字符串
     */
    @NonNull
    public static String splitNumString(int type, String numStr) {
        if (TextUtils.isEmpty(numStr)) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        if (type == 1) {
            numStr = numStr.replace("+86", "");
            if (numStr.length() >= 3) {
                builder.append(numStr.substring(0, 3) + " ");
                String lastStr = numStr.substring(3, numStr.length());
                Matcher m = Pattern.compile("\\d\\d\\d\\d").matcher(lastStr);
                while (m.find()) {
                    builder.append(m.group() + " ");
                }
            }
        } else {
            int index = numStr.length() % 4;
            if (index == 0) {
                Matcher m = Pattern.compile("\\d\\d\\d\\d").matcher(numStr);
                while (m.find()) {
                    builder.append(m.group() + " ");
                }
            } else {
                String lastStr = numStr.substring(numStr.length() - index, numStr.length());
                Matcher m = Pattern.compile("\\d\\d\\d\\d").matcher(numStr);
                while (m.find()) {
                    builder.append(m.group() + " ");
                }
                builder.append(lastStr);
            }
        }
        return builder.toString().trim();
    }

    /**
     * 得到 全拼
     *
     * @param src
     * @return
     */
    public static String getPingYin(String src) {
        char[] t1 = null;
        t1 = src.toCharArray();
        String[] t2 = new String[t1.length];
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);
        String t4 = "";
        int t0 = t1.length;
        try {
            for (int i = 0; i < t0; i++) {
                // 判断是否为汉字字符
                if (java.lang.Character.toString(t1[i]).matches(
                        "[\\u4E00-\\u9FA5]+")) {
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
                    t4 += t2[0];
                } else {
                    t4 += java.lang.Character.toString(t1[i]);
                }
            }
            return t4;
        } catch (BadHanyuPinyinOutputFormatCombination e1) {
            e1.printStackTrace();
        }
        return t4;
    }

    /**
     * 得到首字母
     *
     * @param str
     * @return
     */
    public static String getHeadChar(String str) {

        String convert = "";
        char word = str.charAt(0);
        String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
        if (pinyinArray != null) {
            convert += pinyinArray[0].charAt(0);
        } else {
            convert += word;
        }
        return convert.toUpperCase();
    }

    /**
     * 得到中文首字母缩写
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {

        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
        return convert.toUpperCase();
    }

    /**
     * 判断字符串是否为空
     *
     * @return
     */
    public static boolean isEmpty(String str) {
        if (TextUtils.isEmpty(str) || "null".equals(str)) {
            return true;
        }
        return false;
    }

    /**
     * 从字符串中提取数字的字符串
     *
     * @return
     */
    public static String getNumberString(String string) {
        if (isEmpty(string)) {
            return "";
        }
        String regEx = "[^0-9]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(string);
        return matcher.replaceAll("").toString();
    }

    //金额验证  
    public static boolean isNumber(String str) {
        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");// 判断小数点后2位的数字的正则表达式  
        Matcher match = pattern.matcher(str);
        if (match.matches() == false) {
            return false;
        } else {
            return true;
        }
    }

}
